//#ifdef DESKTOP

core.dock = {};

core.dock.MAX_ELEMENT_SIZE = 25;
core.dock.FLOAT_PANEL_BORDER = 2;
core.dock.FLOAT_PANEL_ZINDEX = 99;
core.dock.Dock = function(config){
    this.init(config);
};

core.extendClass('core.dock.Dock', 'core.Tabs');

core.dock.Dock.prototype.init = function(config){
    config.toolbar = true;
    config.minWidth = core.dock.MAX_ELEMENT_SIZE;
    config.minHeight = core.dock.MAX_ELEMENT_SIZE;
    config.topDown = false;
    this.superCall('init', arguments, config);
}

core.dock.Dock.prototype.removeDockElement = function(aElement){
    this.removeTab(aElement);
};

core.dock.Dock.prototype.addDockElement = function(aElement){
    aElement.vertical = this.vertical;
    aElement.lineup(false);
    this.addTab(aElement);
    if(!this.vertical){
        //calc required width
		var reqWidth = TAB_PADDING;
		for(var i = 0; i<this.tabs.length; i++){
			reqWidth += this.tabs[i].minWidth+TAB_PADDING;
		}
        if(reqWidth<core.desktop.width/2){
            this.minWidth = reqWidth;
        }else{
            this.minWidth = Math.floor(core.desktop.width/2);
        }
        core.desktop.bottomPanel.refresh(true);
    }else{
        this.refresh();
    }
};

core.dock.DockElement = function(config){
    this.init(config);
}

core.extendClass('core.dock.DockElement', 'core.Box');

core.dock.DockElement.prototype.init = function(config){
    config.items = [];
    this.superCall('init', arguments, config);
    this.menu = config.menu;
    this.vertical = config.vertical;
    this.orientationAllowed = config.orientationAllowed || 0;
    this.skipClick = false;

    this.div.oncontextmenu = function(){
        return false;
    };
    var instance = this;
    $(this.div).mousedown(function(e){
        if(instance.parent)
            if(instance.parent.hideDropDown())
                return false;
        var rightClick = $.browser.opera?(e.shiftKey && e.which==1):(e.which==3);
        if(instance.menu && rightClick){
            instance.skipClick = true;
//            core.log('Call menu here, if available');
            instance.parent.menuTab = instance;
            instance.parent.dropDown = new core.Menu({
                menu: instance.menu,
                owner: instance,
                areaWidth: core.desktop.activeElement.width-2*TAB_PADDING,
                areaHeight: core.desktop.activeElement.height-2*TAB_PADDING,
                startPointX: instance.vertical?(core.desktop.activeElement.width-TAB_PADDING):(instance.parent.bounds.x+instance.bounds.x),
                startPointY: instance.vertical?(instance.bounds.y+instance.height):core.desktop.activeElement.height-2,
                parent: core.desktop.activeElement,
                topDown: instance.vertical
            });
            instance.parent.dropDown.on('select', function(i, e, item){
                instance.parent.hideDropDown();
                if(instance.parent.menuTab)
                    instance.parent.menuTab.fire('menu_select', e, instance.parent.selectedTab, item);
            });
//            instance.parent.showDropDown(instance, instance.menu, false, false, core.desktop.bottomPanel);
        }
        return false;
    });
    
    $(this.div).click(function(e){
        if(instance.skipClick){
            instance.skipClick = false;
            return false;
        }
        if(instance.parent)
            instance.parent.hideDropDown();
        instance.fire('click', e);
        return false;
    }).addClass('dock_element');
    this.div.oncontextmenu = function(){
        return false;
    };

    this.elements = [];
    var arr = $.makeArray(config.elements);
    for (var i = 0; i < arr.length; i++) {
        this.addElement(arr[i]);
    }

//    this.lineup(false);
};

core.dock.DockElement.prototype.addElement = function(aElement){
    this.elements.push(aElement);

    $(aElement).addClass('dock_element_element').mousedown(function(e){
        e.preventDefault();
    }).appendTo(this.div);
};

core.dock.DockElement.prototype.lineup = function(aRefresh){
    this.minWidth = 0;
    this.minHeight = 0;
    var size = 0;
    for (var i = 0; i < this.elements.length; i++) {
        var el = this.elements[i];
        if(this.vertical){
            $(el).css('left', 0).css('top', size);
            size += $(el).attr('side') || $(el).height();
        }else{
            $(el).css('left', size).css('top', 0);
            size += $(el).attr('side') || $(el).width();
        }

    }
    if(this.vertical){
        this.minWidth = core.dock.MAX_ELEMENT_SIZE;
        this.minHeight = size;
    }else{
        this.minHeight = core.dock.MAX_ELEMENT_SIZE;
        this.minWidth = size;
    }
    if(this.parent && aRefresh)
        this.parent.refresh();
};

//#endif